from django.http import JsonResponse
from django.shortcuts import render
from app01.tasks import add
from django.shortcuts import HttpResponse
from celery.result import AsyncResult
import random


# Create your views here.
def async_work(request):
    print('...发送异步请求...')
    # 生成用来生成随机数的序列
    num_list = [i for i in range(0, 1000)]
    # 从num_list中随机的获取两个数据
    a, b = random.sample(num_list, 2)
    # 显示随机生成的数字
    print(a, b)
    res = add.delay(a, b)
    print("类型：", type(res), ",任务信息：", res)
    return HttpResponse(("请求是成功，正在生成结果   ", res))


def get_result(request, task_id):
    res = AsyncResult(id=task_id)
    if res.ready():
        return HttpResponse(res.get())
    else:
        return HttpResponse("请稍等...")

def index(request):
    return render(request,"index.html")

def post_data(request):
    print("request Method:",request.method)
    print("请求的参数：",request.POST.get('text'))
    data = {"status":200}
    resp= JsonResponse(data)
    # return HttpResponse("haha")
    return resp


if __name__ == '__main__':
    async_work()
